package Day7;

import java.util.Scanner;
import java.util.Stack;

public class Main {

    public static void main(String[] args) {
        String A = "(()())";
        int n = 6;
        boolean b = chkParenthesis(A, n);
        System.out.println(b);

        A = "()a()()";
        n = 7;
        System.out.println(chkParenthesis(A, n));

        A = "()(()()";
        n = 7;
        System.out.println(chkParenthesis(A, n));
    }

    public static boolean chkParenthesis(String A, int n) {
        char[] chars = A.toCharArray();
        Stack<Character> stack = new Stack<>();

        int i = 0;
        while (n != i) {

            if (chars[i] == '(') {
                stack.push(chars[i]);
            } else if (chars[i] == ')') {
                //没有可以与之匹配的'(', 直接false
                if (stack.empty()) {
                    return false;
                }
                //将')'和栈顶匹配, 不成功就false, 成功就继续
                if (stack.pop() != '(') {
                    return false;
                }
            }
            //非括号直接false
            else{
                return false;
            }
            i++;
        }

        //空栈ok
        return stack.empty();
    }

    public static void main1(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();

        int i = 0;
        while (getFib(i) < N) {
            i++;
        }

        int n = Math.abs(getFib(i) - N);
        int m = Math.abs(getFib(i - 1) -N);

        System.out.println(Math.min(n, m));

    }

    public static int getFib (int n) {
        if (n <= 1) {
            return n;
        } else {
            return getFib(n-1) + getFib(n - 2);
        }
    }

}
